package com.cards_game.simple_ddz.verify;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import com.cards_game.simple_ddz.bean.BaseCardBean;
import com.cards_game.simple_ddz.common.RoomBean;
import com.cards_game.simple_ddz.constants.CommonConstants;
import com.cards_game.simple_ddz.rule.BaseVerify;
import com.cards_game.simple_ddz.rule.CardSortUtil;

/**
 * 出牌校验
 * 
 * @author 郭国辉 2016年3月17日 下午5:14:13
 */
public class SimpleDdzVerify extends BaseVerify{
	
	private static SimpleDdzVerify sdv=new SimpleDdzVerify();
	private static List<SimpleDdzRuleEnum> list = new ArrayList<>();

	private SimpleDdzVerify() {
		if(list==null){
			list=new ArrayList<>(12);
		}
		list.add(SimpleDdzRuleEnum.SINGLE);
		list.add(SimpleDdzRuleEnum.PAIR);
		list.add(SimpleDdzRuleEnum.THREE);
		list.add(SimpleDdzRuleEnum.THREE_ONE);
		list.add(SimpleDdzRuleEnum.THREE_TWO);
		list.add(SimpleDdzRuleEnum.SERIAL);
		list.add(SimpleDdzRuleEnum.SHIFTED_PAIRS);
		list.add(SimpleDdzRuleEnum.INCOMPLETE_AIRCRAFT);
		list.add(SimpleDdzRuleEnum.AIRCRAFT);
		list.add(SimpleDdzRuleEnum.BOMB);
		list.add(SimpleDdzRuleEnum.KINGS_BOMB);
	}
	
	public static SimpleDdzVerify getSimpleDdzVerify(){
		return sdv;
	}
	/**
	 * 检验出牌是否符合规则
	 * 
	 * @param baseCardBeans
	 * @return
	 */
	public boolean discardCards(List<BaseCardBean> simpleDdzBeans) {
		for(SimpleDdzRuleEnum simpleDdzRuleEnum:list){
			if(simpleDdzRuleEnum.discardCards(simpleDdzBeans)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断出牌是否大于上家
	 * @param lastCardBeans
	 * @param thisCardBeans
	 * @return
	 */
	public boolean canPlayCards(List<BaseCardBean> lastCardBeans, List<BaseCardBean> thisCardBeans) {
		SimpleDdzRuleEnum simpleDdzRuleEnum=getRule(lastCardBeans);
		boolean boo=simpleDdzRuleEnum.canPlayCards(lastCardBeans, thisCardBeans);
		return boo;
	}
	
	/**
	 * 获取符合出牌规则的枚举对象
	 * @param intactCardBeans
	 * @return
	 */
	public SimpleDdzRuleEnum getRule(List<BaseCardBean> simpleDdzBeans){
		for(SimpleDdzRuleEnum simpleDdzRuleEnum:list){
			if(simpleDdzRuleEnum.discardCards(simpleDdzBeans)){
				return simpleDdzRuleEnum;
			}
		}
		return null;
	}


	private CardSortUtil cardSortUtil = new CardSortUtil();

	/**
	 * 判断出牌是否是连牌
	 * 
	 * @param baseCardBeans
	 * @return
	 */
	public boolean isSerial(List<BaseCardBean> baseCardBeans) {
		Set<Integer> numSet = getCardsNumList(baseCardBeans);
		if (numSet != null && numSet.size() != 0) {
			if (baseCardBeans.size() == numSet.size()) {
				return isSerial(numSet);
			}
		}
		return true;
	}

	/**
	 * 判断出牌是否是连续的
	 * 
	 * @param baseCardBeans
	 * @return
	 */
	public boolean isSerial(Set<Integer> numSet) {
		if (numSet != null && numSet.size() != 0) {
			Iterator<Integer> it = numSet.iterator();
			int start = it.next();
			while (it.hasNext()) {
				if (start + 1 != it.next()) {
					start = it.next();
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 获取斗地主中每个牌面值的权重
	 * 
	 * @param num
	 * @return
	 */
	public int getWeight(int num) {
		if (num > 2) {
			return num - 2;
		} else if (num != -1) {
			return num + 11;
		} else {
			return 14;
		}
	}

	/**
	 * 获取牌面值的权重集合
	 * 
	 * @param baseCardBeans
	 * @return
	 */
	public Set<Integer> getCardsNumList(List<BaseCardBean> baseCardBeans) {

		Set<Integer> numSet = cardSortUtil.getCardsNumList(baseCardBeans);
		Set<Integer> newSet = new TreeSet<Integer>();
		for (BaseCardBean bcb : baseCardBeans) {
			newSet.add(getWeight(bcb.getNum()));
		}
		return numSet;
	}

}
